﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FTchina.Library;
namespace FTchina.Logic
{
    /// <summary>
    ///TelnetLogic 的摘要说明
    /// </summary>
    public class TelnetLogic
    {
        public TelnetLogic()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 封装执行no arp 操作
        /// </summary>
        /// <param name="routerIP"></param>
        /// <param name="IPs"></param>
        public static void ExecNoArp(string routerIP, string IPs)
        {
            //调用方法如下：
            Telnet p = new Telnet(routerIP, 23, 100);

            if (p.Connect() == false)
            {
                LogHelper.Info(routerIP + "telnet连接失败");
            }
            //等待指定字符返回后才执行下一命令
            LoginRouter(p);
            NoArp(p, IPs);
            //Response.Write(p.SessionLog);
            //Response.Write(p.WorkingData);

            LogHelper.Info(routerIP + "-" + IPs);

        }



        /// <summary>
        /// no arp 命令,ip多个以英文逗号分隔
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ip">134.44.36.22,134.44.36.28</param>
        private static void NoArp(Telnet p, string ip)
        {
            p.Send("config t");
            p.WaitFor("#");
            if (ip.Contains(","))
            {
                string[] ips = ip.Split(',');
                foreach (string str in ips)
                {
                    p.Send("no arp  " + str);
                }
            }
            else
            {
                p.Send("no arp  " + ip);
            }
            p.WaitFor("#");
            p.Send("end");
            p.WaitFor("#");

            //p.Send("show arp | in  10.73.211.77 ");
            //p.WaitFor("#");
        }

        /// <summary>
        /// 登录到交换机
        /// </summary>
        /// <param name="p"></param>
        private static void LoginRouter(Telnet p)
        {
            p.WaitFor("Username:");
            p.Send("weibaohui");
            p.WaitFor("Password:");
            p.Send("baohui");
            p.WaitFor(">");
            p.Send("en");
            p.WaitFor("Password:");
            p.Send("baohui");
            p.WaitFor("#");
        }
    }
}